package com.example.module_message.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.module_message.R
import com.example.module_message.model.MessageEntity

class MessageAdapter : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {
    var messageList: List<MessageEntity> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
        return MessageViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
        )
    }
    fun addMessageList(list: List<MessageEntity>) {
        messageList = list
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
        holder.type.text = messageList[position].messageType
        holder.content.text = messageList[position].messageContent
        holder.time.text = messageList[position].sendTime.substring(10, 15)
        holder.status.text = messageList[position].messageStatus
//        when (messageList[position].messageStatus) {
//            "已读" -> {
//                holder.status.setBackgroundResource(R.drawable.shape_message_read)
//            }
//            "未读" -> {
//                holder.status.setBackgroundResource(R.drawable.shape_message_unread)
//            }
//        }

        //系统通知、物业服务、车位管理等
        when (messageList[position].messageType) {
            "系统通知" -> {
                holder.icon.setBackgroundResource(R.drawable.system_notice)
            }
            "物业服务" -> {
                holder.icon.setBackgroundResource(R.drawable.wuye_service)
            }
            "车位管理" -> {
                holder.icon.setBackgroundResource(R.drawable.car_manager)
            }
        }
    }

    override fun getItemCount() = messageList.size
    inner class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val type: TextView = itemView.findViewById(R.id.type)
        val content: TextView = itemView.findViewById(R.id.content)
        val time: TextView = itemView.findViewById(R.id.time)
        val status: TextView = itemView.findViewById(R.id.status)
        val icon: ImageView = itemView.findViewById(R.id.icon)
    }
}

